package com.cat.model.web.action.admin.acnt;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;

import com.alonew.core.commons.page.Page;
import com.alonew.db.dao.TxCommonDAO;
import com.cat.model.common.model.common.AcntWithdrawVO;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.dao.mapper.ModelMapper;
import com.cat.model.web.action.base.AdminAction;
import com.cat.model.web.bean.AcntWithdrawBean;

public class AcntWithdrawManagerAction extends AdminAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 425636316597986569L;
	
	private Long id;
	private String approveRemark;
	private String userId;
	private String approveStatus;
	private Date createBeginTime;
	private Date createEndTime;
	private BigDecimal minAmount;
	private BigDecimal maxAmount;
	private String bankAcntName;
	private String bankAcntNo;
	private String bankName;
		
	@Autowired
	private PlmCommDAO plmCommDAO;
	
	@Autowired
	private TxCommonDAO commonDAO;
	
	@Autowired
	private ModelMapper modelMapper;
	
	/**
	 * list
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String list() {
		Map<String, Object> condition = new HashMap<String, Object>();
		
		if(StringUtils.isNotEmpty(userId)){
			condition.put("userId", userId);
		}
		
		if(StringUtils.isNotEmpty(approveStatus)){
			condition.put("approveStatus", approveStatus);
		}
		
		if(createBeginTime!=null){
			condition.put("createBeginTime", createBeginTime);
		}
		
		if(createEndTime!=null){
			condition.put("createEndTime", createEndTime);
		}
		
		if(minAmount != null){
			condition.put("minAmount", minAmount);
		}
		
		if(maxAmount != null){
			condition.put("maxAmount", maxAmount);
		}
		
		if(StringUtils.isNotEmpty(bankAcntName)){
			condition.put("bankAcntName", bankAcntName);
		}
		
		if(StringUtils.isNotEmpty(bankAcntNo)){
			condition.put("bankAcntNo", bankAcntNo);
		}
		
		if(StringUtils.isNotEmpty(bankName)){
			condition.put("bankName", bankName);
		}
		
		Page page = this.fetchPageParams();
		page.setNumPerPage(20);
		
		Long totalCount = plmCommDAO.searchSize("manager.searchAcntWithdrawSize", condition);
		page.setTotalCount(totalCount);
		int start = (page.getPageNum() -1) * page.getNumPerPage();
		if(start < 0){
			start = 0;
		}
		
		List<AcntWithdrawBean> list = (List<AcntWithdrawBean>)plmCommDAO.search("manager.searchAcntWithdraw", condition, start, page.getNumPerPage());
		
		request.setAttribute("list", list);
		request.setAttribute("size", totalCount);
		return "list";
	}
	
	public String detail() {
		AcntWithdrawBean AcntWithdraw = plmCommDAO.getAcntWithdraw(id);
		request.setAttribute("item", AcntWithdraw);
		return "detail";
	}
	
	public String toPass(){
		return "pass";
	}
	
	public String doPass(){
		try {
			AcntWithdrawVO acntWithdraw = modelMapper.getAcntWithdraw(id);
			if(!"01".equals(acntWithdraw.getApproveStatus())){
				parentAlert(false, "状态已改变无法复核，请确认！");
				return "list";
			}
			acntWithdraw.setApproveRemark(approveRemark);
			acntWithdraw.setApproveTime(new Date());
			acntWithdraw.setApproveStatus("03");//03-通过
			commonDAO.updateById(acntWithdraw, "approveRemark","approveTime","approveStatus");
		} catch (Exception e) {
			e.printStackTrace();
			parentAlert(false, e.getMessage());
			return "list";
		}
		parentAlert(true, "审核成功");
		return "list";
	}
	
	public String toRevoke(){
		return "revoke";
	}
	
	public String doRevoke(){
		try {
			AcntWithdrawVO acntWithdraw = modelMapper.getAcntWithdraw(id);
			if(!"01".equals(acntWithdraw.getApproveStatus())){
				parentAlert(false, "状态已改变无法复核，请确认！");
				return "list";
			}
			acntWithdraw.setApproveRemark(approveRemark);
			acntWithdraw.setApproveTime(new Date());
			acntWithdraw.setApproveStatus("02");//02-驳回
			commonDAO.updateById(acntWithdraw, "approveRemark","approveTime","approveStatus");
		} catch (Exception e) {
			e.printStackTrace();
			parentAlert(false, e.getMessage());
			return "list";
		}
		parentAlert(true, "审核成功");
		return "list";
	}
	
	public String toSuccess(){
		return "success";
	}
	
	public String doSuccess(){
		try {
			AcntWithdrawVO acntWithdraw = modelMapper.getAcntWithdraw(id);
			if(!"03".equals(acntWithdraw.getApproveStatus())){
				parentAlert(false, "状态已改变无法复核，请确认！");
				return "list";
			}
			acntWithdraw.setApproveRemark(approveRemark);
			acntWithdraw.setApproveTime(new Date());
			acntWithdraw.setApproveStatus("04");//04-已转账
			commonDAO.updateById(acntWithdraw, "approveRemark","approveTime","approveStatus");
		} catch (Exception e) {
			e.printStackTrace();
			parentAlert(false, e.getMessage());
			return "list";
		}
		parentAlert(true, "审核成功");
		return "list";
	}
	
	@SuppressWarnings({ "unchecked", "deprecation" })
	public String exportExcel(){
		// 第一步，创建一个webbook，对应一个Excel文件  
        HSSFWorkbook wb = new HSSFWorkbook();  
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet  
        HSSFSheet sheet = wb.createSheet("acntWithdraw");  
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
        HSSFRow row = sheet.createRow((int) 0);  
        // 第四步，创建单元格，并设置值表头 设置表头居中  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
  
        HSSFCell cell = row.createCell((short) 0);  
        cell.setCellValue("ID");  
        cell.setCellStyle(style);  
        cell = row.createCell((short) 1);  
        cell.setCellValue("用户ID");  
        cell.setCellStyle(style);  
        cell = row.createCell((short) 2);  
        cell.setCellValue("申请时间");  
        cell.setCellStyle(style); 
        cell = row.createCell((short) 2);  
        cell.setCellValue("公司名称");  
        cell.setCellStyle(style); 
        cell = row.createCell((short) 3);  
        cell.setCellValue("用户昵称");  
        cell.setCellStyle(style); 
        cell = row.createCell((short) 4);  
        cell.setCellValue("用户真实姓名");  
        cell.setCellStyle(style); 
        cell = row.createCell((short) 5);  
        cell.setCellValue("提现金额");  
        cell.setCellStyle(style); 
        cell = row.createCell((short) 6);  
        cell.setCellValue("户名");  
        cell.setCellStyle(style); 
        cell = row.createCell((short) 7);  
        cell.setCellValue("账号");  
        cell.setCellStyle(style); 
        cell = row.createCell((short) 8);  
        cell.setCellValue("银行");  
        cell.setCellStyle(style); 
        cell = row.createCell((short) 9);  
        cell.setCellValue("申请时间");  
        cell.setCellStyle(style); 
        cell = row.createCell((short) 10);  
        cell.setCellValue("状态");  
        cell.setCellStyle(style); 
        
        
        // 第五步，写入实体数据 实际应用中这些数据从数据库得到， 
        Map<String, Object> condition = new HashMap<String, Object>();
		
        if(StringUtils.isNotEmpty(userId)){
			condition.put("userId", userId);
		}
		
		if(StringUtils.isNotEmpty(approveStatus)){
			condition.put("approveStatus", approveStatus);
		}
		
		if(createBeginTime!=null){
			condition.put("createBeginTime", createBeginTime);
		}
		
		if(createEndTime!=null){
			condition.put("createEndTime", createEndTime);
		}
		
		if(minAmount != null){
			condition.put("minAmount", minAmount);
		}
		
		if(maxAmount != null){
			condition.put("maxAmount", maxAmount);
		}
		
		if(StringUtils.isNotEmpty(bankAcntName)){
			condition.put("bankAcntName", bankAcntName);
		}
		
		if(StringUtils.isNotEmpty(bankAcntNo)){
			condition.put("bankAcntNo", bankAcntNo);
		}
		
		if(StringUtils.isNotEmpty(bankName)){
			condition.put("bankName", bankName);
		}
		
		Page page = this.fetchPageParams();
		
		Long totalCount = plmCommDAO.searchSize("manager.searchAcntWithdrawSize", condition);
		page.setTotalCount(totalCount);
		page.setNumPerPage(totalCount.intValue());
		int start = 0;
		
		List<AcntWithdrawBean> list = (List<AcntWithdrawBean>)plmCommDAO.search("manager.searchAcntWithdraw", condition, start, page.getNumPerPage());
  
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		int rowNo = 1;
		for (int i = 0; i < list.size(); i++)  
        {  
            row = sheet.createRow((int)rowNo);  
            AcntWithdrawBean bean = (AcntWithdrawBean) list.get(i);  
            // 第四步，创建单元格，并设置值  
            row.createCell((short) 0).setCellValue(bean.getId());  
            row.createCell((short) 1).setCellValue(bean.getUserId()); 
            row.createCell((short) 2).setCellValue(bean.getCompanyName()); 
            row.createCell((short) 3).setCellValue(bean.getNickName()); 
            row.createCell((short) 4).setCellValue(bean.getRealName()); 
            if(bean.getAmount()!=null){
            	row.createCell((short) 5).setCellValue(bean.getAmount().doubleValue()); 
            }
            row.createCell((short) 6).setCellValue(bean.getBankAcntName()); 
            row.createCell((short) 7).setCellValue(bean.getBankAcntNo()); 
            row.createCell((short) 8).setCellValue(bean.getBankName()); 
            row.createCell((short) 9).setCellValue(sdf.format(bean.getCreateTime()));
            String status = bean.getApproveStatus();
            String statusName = "";
            if("01".equals(status)){
            	statusName = "待审核";
            }  else if("02".equals(status)){
            	statusName = "驳回";
            }  else if("03".equals(status)){
            	statusName = "审核通过";
            } else if("04".equals(status)){
            	statusName = "已转账";
            }
            row.createCell((short) 10).setCellValue(statusName); 
            rowNo ++;
        }  
        // 第六步，将文件存到流中 
        try  
        {  
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            wb.write(os);
            byte[] fileContent = os.toByteArray();
            ByteArrayInputStream is = new ByteArrayInputStream(fileContent);

            excelStream = is;             //文件流
            excelFileName = "acntWithdraw.xls"; //设置下载的文件名
        }  
        catch (Exception e) {  
            e.printStackTrace();
        }
		return "success";
	}
	
	private InputStream excelStream;  //输出流变量
    private String excelFileName; //下载文件名

    public InputStream getExcelStream() {
        return excelStream;
    }
    public void setExcelStream(InputStream excelStream) {
        this.excelStream = excelStream;
    }
    public String getExcelFileName() {
        return excelFileName;
    }
    public void setExcelFileName(String excelFileName) {
        this.excelFileName = excelFileName;
    }

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getApproveRemark() {
		return approveRemark;
	}

	public void setApproveRemark(String approveRemark) {
		this.approveRemark = approveRemark;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getApproveStatus() {
		return approveStatus;
	}

	public void setApproveStatus(String approveStatus) {
		this.approveStatus = approveStatus;
	}

	public Date getCreateBeginTime() {
		return createBeginTime;
	}

	public void setCreateBeginTime(Date createBeginTime) {
		this.createBeginTime = createBeginTime;
	}

	public Date getCreateEndTime() {
		return createEndTime;
	}

	public void setCreateEndTime(Date createEndTime) {
		this.createEndTime = createEndTime;
	}

	public BigDecimal getMinAmount() {
		return minAmount;
	}

	public void setMinAmount(BigDecimal minAmount) {
		this.minAmount = minAmount;
	}

	public BigDecimal getMaxAmount() {
		return maxAmount;
	}

	public void setMaxAmount(BigDecimal maxAmount) {
		this.maxAmount = maxAmount;
	}

	public String getBankAcntName() {
		return bankAcntName;
	}

	public void setBankAcntName(String bankAcntName) {
		this.bankAcntName = bankAcntName;
	}

	public String getBankAcntNo() {
		return bankAcntNo;
	}

	public void setBankAcntNo(String bankAcntNo) {
		this.bankAcntNo = bankAcntNo;
	}

	public String getBankName() {
		return bankName;
	}

	public void setBankName(String bankName) {
		this.bankName = bankName;
	}


}
